<?php

namespace App\V3\V3_2_7\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BarterModel extends Model
{


    use HasFactory;
    protected $table = 'barter';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * $dates 数组内的列转换为日期格式
     * @var array
     */
    protected $dates = [
        'barter_date',
        'created_at',
        'deliver_time',
        'collection_time'
    ];

    public function barterGoodsDetailInfo()
    {
        return self::hasMany(BarterGoodsDetailModel::class, 'barter_id', 'id');
    }

    public function driverInfo()
    {
        return self::hasMany(BarterDriverModel::class, 'barter_id', 'id');
    }

    public function barterLogisticsInfo(){
        return self::hasMany(BarterLogisticsInfoModel::class, 'barter_id', 'id');

    }

    /**
     *
     * @param unknown $userId
     * @param unknown $data
     * @return unknown
     */
    static public function getBarterList($userId, $data){
        return SELF::select('id')
        ->where([
            'delete_flag'=>0,
        ])->where($userId)
        ->where(function($query) use($data){
            if($data['search_type'] == 3){
                $query->where('order_no', 'like', '%'.$data['keyword'].'%');
            }elseif ($data['search_type'] == 4){
                $query->where('out_order_no', 'like', '%'.$data['keyword'].'%');
            }elseif($data['search_type'] == 6){
                $query->where('customer_name', 'like', '%'.$data['keyword'].'%');
            }else{
                $query->where('deliver_person', 'like', '%'.$data['keyword'].'%');
            }
        })->get()->toArray();
    }

    /**
     * 查询换货信息
     * @param unknown $id
     */
    static public function getBarterInfo($id)
    {
        return self::select(
            'a.*',
            'b.username',
            'b.nickname',
            'a.id as barter_id',
            'c.nickname as approver_name'
        )->from('barter as a')
        ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
        ->leftJoin('admin as c', 'b.id', '=', 'a.approver_id')
        ->with('driverInfo', function($query) {
            $query->select(
                'id',
                'plate_number',
                'name',
                'phone',
                'logistics_company',
                'remark',
                'plate_image',
                'id_card_image',
                'driving_licence',
                'barter_id'
                );
        })->with('barterLogisticsInfo', function($query) {
            $query->select(
                'id',
                'barter_id',
                'logistics_company',
                'logistics_order_no'
                )->where('delete_flag',0);
        })
        ->where([
            'a.id' => $id,
            'a.delete_flag' => 0
        ])->first()->toArray();
    }

    /**
     * 快速出入库同步价格
     * @param Request $data
     */
    static public function getSyncPrice($data)
    {
        $idArr = explode(',', $data['id']);
        $barterData = [];
        if(count($idArr) > 0){
            foreach ($idArr as $key => $gid){
                $barterInfo = SELF::query()
                    ->select(
                    'price',
                    'b.type_id as id',
                    'c.cost as cost_price'
                )->from('barter as a')
                    ->leftJoin(
                        'barter_goods_detail as b',
                        'b.barter_id','=','a.id'
                    ) ->leftJoin(
                        'materiel_type as c',
                        'b.type_id','=','c.id'
                    )->where([
                        'a.delete_flag' => 0,
                        'b.repeal_status' => 0,
                        'a.main_id' => $data['main_id']
                    ])->where('type_id',$gid);
                if(!empty($data['customer_id'])){
                    $barterInfo->where('customer_id',$data['customer_id']);
                }
                $barterInfo = $barterInfo->orderBy('a.id','desc')->first();
                $barterData[$key] = [
                    'price' => '',
                    'id' => $gid,
                    'cost_price' => ''
                ];
                if(!empty($barterInfo)){
                    $barterData[$key] = $barterInfo->toArray();
                }
            }
        }
        return $barterData;
    }
}
